import java.awt.*;
import java.awt.Graphics2D;
import java.awt.image.*;
import java.util.Vector;

public class Renderer
{
	/*The Renderer class gathers the data from the assorted repositories in the simulation 
	 * and processes it into an image then writes that image to tthe map window.
	 */ 
	static BufferedImage b = new BufferedImage(SimulationConstants.simsize, SimulationConstants.simsize, BufferedImage.TYPE_INT_RGB);


	static int[][] map;
	static int[][] T;
	static int[][] H;
	static loc[][] W;
	static loc[][] smallermap;
	static int[][] P;
	static int windresolution = 32;
	static Vector A;
	static public void StartRender()
	{
		// for(int i=0; i<20;i++) 

		map = HeightMap.GetHeightMap();
		T = TempMap.GetTempData();
		H = HumidMap.GetHumidData();
		W = WindMap.GetWindData();
		P = PlantMap.GetPlantData();
		A = AnimalHandlerObject.getAnimalVector();



		//Processing goes here to fill bitmap
		//OnscreenMap.Render(DoStuff()); //FIX!
	}

	public static BufferedImage DoStuff(){

		//System.out.println("DO STUFF!");//test

		Magic_Eight_Ball.prove_Renderer_Updates();//This exists for the testing
		StartRender();
		String[] Constants = SimulationConstants.GetSimulationConstants();
		Graphics2D g = b.createGraphics();
		WritableRaster raster = b.getRaster();
		int x=0;
		int y=0;
		if(Constants[0]=="wind")
		{
			////System.out.println("Starting poll");
			smallermap = new loc[512/8][512/8];
			//initalize the smallermap
			for(int x2=0;x2<512/windresolution;x2++)
			{
				for(int y2=0; y2<512/windresolution;y2++)
				{     
					smallermap[x2][y2]= new loc(0,0);
				}
			}
			//Make the averages
			////System.out.println("Starting windAverage");
			int total = 0;
			for(int x2=0;x2<512;x2++)
			{
				for(int y2=0; y2<512;y2++)
				{
					smallermap[x2/windresolution][y2/windresolution].x+=W[x2][y2].x;
					smallermap[x2/windresolution][y2/windresolution].y+=W[x2][y2].y;

				}
			}


		}
		while(x<SimulationConstants.simsize)
		{
			while(y<SimulationConstants.simsize)
			{
				Color color = new Color(0,0,0);
				if(Constants[0]=="normal")
				{
					if(map[x][y]>255)
						color = new Color(255,255,255);

					else if(map[x][y]<SimulationConstants.sealevel)
						color = new Color(10,10,200);
					else
					{
						if(map[x][y]<60)
							color = new Color((int)(255*map[x][y]/60),(int)(255*map[x][y]/60),0);
						else if(map[x][y]<120)
							color = new Color((int)(255-95*map[x][y]/120),(int)(255-180*map[x][y]/120),(int)(20*map[x][y]/120));
						else if(map[x][y]<255)
							color = new Color((int)(140+115*map[x][y]/255),(int)(70+185*map[x][y]/255),(int)(20+235*map[x][y]/255));
						//139;69;19


					}

					/*
					 * black->yellow->brown->white
					 * 0->60->120->240
					 * 
					 * 
					 */ 



				}
				else if(Constants[0]=="temperature")
				{
					double temp = (T[x][y]>-100)?(double)T[x][y]:-100.0;
					temp = (temp<200)?(double)temp:300.0;
					if(temp<0.0)
						color = new Color(0,0,(int)(255*((100.0+temp)/100)));
					else if(temp<=100)
						color = new Color((int)(255*(temp/100)),0,0);
					else if(temp<=200)
						color = new Color(255,(int)(255*(temp/200)),0);
					else if(temp<=450)
						color = new Color(255,255,(int)(255*(temp/450.0)));                         
				}
				else if(Constants[0]=="plants")
				{
					double temp = (P[x][y]>0)?(double)P[x][y]:0.0;
					temp = (temp<100)?(double)temp:100.0;

					color = new Color((int)(255*(temp/500)),(int)(255*(temp/100)),(int)(255*(temp/500))); 
				}
				else if(Constants[0]=="humidity")
				{
					double temp = (H[x][y]>0)?(double)H[x][y]:0.0;
					temp = (temp<200)?(double)temp:200.0;

					color = new Color((int)(255*(temp/230)),(int)(255*(temp/230)),(int)(255*(temp/200))); 
				}        
				else
					color = new Color(0,0,0);//should run only for the wind

				g.setColor(color);
				g.fillRect(x, y, 1, 1);
				y++;
			}
			x++;
			y=0;


		}
		Vector<AnimalObject> A = AnimalHandlerObject.getAnimalVector();
		for(int i=0; i< A.size(); i++)
		{
			Color color = new Color(0,0,0);
			if(A.get(i).getType()==true)
				color = new Color(200,0,0);
			else
				color = new Color(220,220,0);
			if(!A.get(i).getLiving())
				color = new Color(219,112,147);
			g.setColor(color);
			int size =  A.get(i).getMass()/10 + 4;
			if(size>7)
				size=7;
			if(size < 4)
				size=4;
			g.fillRect(A.get(i).getLocation().x, A.get(i).getLocation().y, size, size);            
		}
		if(Constants[0]=="wind")
		{
			////System.out.println("Starting windpaint");
			Color wind = new Color(255,255,255);
			g.setColor(wind);
			int windmultiplier = 1;
			for(int x2=0;x2<512/windresolution;x2++)
			{
				for(int y2=0; y2<512/windresolution;y2++)
				{
					int magnitude = (int)( Math.sqrt(smallermap[x2][y2].y*smallermap[x2][y2].y+smallermap[x2][y2].x*smallermap[x2][y2].x)/32.0);
					if(magnitude==0)
						magnitude=1;//solves devide by 0 error and does not effect the math.
					smallermap[x2][y2].x/=magnitude;
					smallermap[x2][y2].y/=magnitude;
					g.drawLine(x2*windresolution,y2*windresolution,x2*windresolution+smallermap[x2][y2].x*windmultiplier,y2*windresolution+smallermap[x2][y2].y*windmultiplier);

				}
			}
		}
		//////System.out.println("map done");
		return(b);

	}

}

